<?php
/**
 * Controller to manage users in the admin area
 *
 * @author Konstantin Shamko <konstantin.shamko@gmail.com> 
 * @version 0.0.1
 * @copyright  Copyright (c) 2009 Konstantin Shamko
 * @category VaselinEngine
 * @package Usermanagement Module
 * @subpackage Controller
 * @license  New BSD License
 * 
 */
class admin_UsersController extends Bel_Controller_Aed {	
	/**
	 * Initialization
	 *
	 */
	function init() {
		$this->_model = new Models_Usermanagement_Users ( );
		
		if($this->_request->getParam('user_id')){
			$this->_formname = 'users_edit_form';
		}else{
			$this->_formname = 'users_form';
		}
		
		parent::init ();
		$this->_items_on_page = 25;
	}	
	/**
	 * Populates groups select field with options
	 *
	 * @param Bel_Forms_Builder $form
	 */
	protected function _prepareForm(Bel_Forms_Builder $form) {
		$groups = new Models_Usermanagement_Groups ( );
		$groups = $groups->getAll ('is_guest<>1');
		
		$options = array ();
		foreach ( $groups as $val ) {
			$options [$val->user_group_id] = $val->user_group_name;
		}
		$form->addSubElement ( 'user_group_id', 'options', $options );
		
	}	
	/**
	 * Right before dispalying add/edit form clear password & confirm password fields values 
	 *
	 * @param Bel_Forms_Builder $form
	 */
	protected function _predisplayForm(Bel_Forms_Builder $form){
		$form->addAttributes('user_password',array('value'=>''));
		$form->addAttributes('password2',array('value'=>''));
	}	
	/**
	 * Before adding/updating user validates if username & email are available, 
	 * validate password & confirmation match. If all validations ok: encript 
	 * password using md5 - if password is in the request OR setting hashed password 
	 * field from db - if password is not in the request. Also if user_active is not in the request
	 * set user_active request value to 0.
	 */
	protected function _beforeModify(){
		$error = false;
		
		$oValidate = new Helpers_Usermanagement_Validate($this->_model->info('name'));
		
		if(!$oValidate->ifUsernameAvailable($this->_request->getParam('user_login'), $this->_request->getParam('user_id'))){
			$this->_messages->setMessage('User with such username already exists','error','user_login');
			$error = true;
		}

		if(!$oValidate->ifEmailAvailable($this->_request->getParam('user_email'), $this->_request->getParam('user_id'))){
			$this->_messages->setMessage('User with such email already exists','error','user_email');
			$error = true;
		}		
		
		if(!$oValidate->ifPasswordMatch($this->_request->getParam('user_password'),$this->_request->getParam('password2'))){
			$this->_messages->setMessage('Passwords entered do not match','error','user_password');
			$error = true;
		}		
		
		if($error){
			$this->_formdata->request = $this->_request->getParams();
			$this->_redirect($this->_form_action.'/user_id/'.$this->_request->getParam('user_id'));
		}else{
		
			if(!$this->_request->getParam('user_active')){
				$this->_request->setParam('user_active', 0);
			}
		
			if($this->_request->getParam('user_password')){
				$this->_request->setParam('user_password',md5($this->_request->getParam('user_password')));
			}else{
				$this->_request->setParam('user_password',new Zend_Db_Expr('user_password'));
			}		
		}
	}	
}